from iClouds.apiSerializers import *
from iClouds.apiExceptions import *


class AllinpayWxchatPosSerializer(serializers.ModelSerializer):
    id = IntegerField(label='ID', read_only=True)
    name = CharField(allow_blank=True, allow_null=True,
                     max_length=255, help_text="名称")
    mode = ChoiceField(
        choices=(
            "公众号",
            "小程序",
            "微信支付"
        ),
        default="公众号",
        help_text="类型"
    )
    appid = CharField(allow_blank=True, allow_null=True, max_length=255, required=False,
                      help_text="appid", validators=[UniqueValidator(queryset=AllinpayWxchatPos.objects.all())])
    secret = CharField(allow_blank=True, allow_null=True,
                       max_length=255, required=False, help_text="secret")
    apiv1 = CharField(allow_blank=True, allow_null=True,
                      max_length=255, required=False)
    apiv2 = CharField(allow_blank=True, allow_null=True,
                      max_length=255, required=False)
    apiv3 = CharField(allow_blank=True, allow_null=True,
                      max_length=255, required=False)
    certificate = CharField(allow_blank=True, allow_null=True,
                            max_length=255, required=False, help_text="证书路径")
    parent = IntegerField(allow_null=True, max_value=2147483647,
                          min_value=-2147483648, required=False, help_text="公众号ID")

    class Meta:
        model = AllinpayWxchatPos
        fields = '__all__'


class AllinpayWxpayListSerializer(serializers.ModelSerializer):
    id = IntegerField(label='ID', read_only=True)
    name = CharField(allow_blank=True, allow_null=True,
                     max_length=255,  help_text="费率名称", validators=[UniqueValidator(queryset=AllinpayWxpayList.objects.all())])
    thali = IntegerField(allow_null=True,  min_value=0, help_text="套餐金额(分)")
    meal = IntegerField(allow_null=True, min_value=0, help_text="套餐时长(分钟)")
    excer = IntegerField(allow_null=True, min_value=0, help_text="套餐外单价(分/分钟)")
    deposit = IntegerField(allow_null=True, min_value=0, help_text="押金(分)")
    create = DateTimeField(read_only=True, help_text="修改时间")

    class Meta:
        fields = '__all__'
        model = AllinpayWxpayList


class AllinpayWxpayCurlSerializer(serializers.ModelSerializer):
    id = IntegerField(label='ID', read_only=True)
    cur = IntegerField(allow_null=True,  help_text="当前费率")
    lasttime = DateTimeField(read_only=True, help_text="修改时间")

    class Meta:
        fields = '__all__'
        model = AllinpayWxpayCurl

    def validate_cur(self, value):
        try:
            r = AllinpayWxpayList.objects.get(id=value)
        except BaseException as err:
            raise ValidationError("验证当前费率失败")
        else:
            return value


class SetAllinpayWxpayCurlSerializer(serializers.Serializer):
    cur = IntegerField(allow_null=True,  help_text="当前费率")

    class Meta:
        fields = '__all__'

    def validate_cur(self, value):
        try:
            r = AllinpayWxpayList.objects.get(id=value)
        except BaseException as err:
            raise ValidationError("验证当前费率失败")
        else:
            return value
# print(repr(AllinpayWxpayCurlSerializer()))
